using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class CanMovePlayer : CanMoveBlock
{
    NavMeshAgent nav;
    int StoneLayer;

    private void Awake()
    {
        nav = GetComponent<NavMeshAgent>();
        StoneLayer = LayerMask.GetMask("Stone");
    }

    protected override void SetPosition(Vector3 pos)
    {
        if(nav.enabled)
        {
            nav.Warp(pos);
        }
        else
        {
            transform.position = pos;
        }
    }

    protected override void MoveToForward()
    {
        Ray ray1 = new Ray(transform.position + Vector3.up * 0.5f, Vector3.forward);
        Ray ray2 = new Ray(transform.position + Vector3.up * 0.5f, Vector3.back);
        if (Physics.Raycast(ray2, 100, StoneLayer | layer | treeLayer))
        {
            IsOverlap = true;
            SetPosition(new Vector3(OrigPos.x, OrigPos.y, - maxXZ + 6));
        }
        else
        {
            if (Physics.Raycast(ray1, 100, StoneLayer | layer | treeLayer))
            {
                SetPosition(new Vector3(OrigPos.x, OrigPos.y, - maxXZ - 6));
                IsOverlap = true;
            }
            else
            {
                SetPosition(new Vector3(OrigPos.x, OrigPos.y, - maxXZ));
            }
        }
    }

    protected override void MoveToRight()
    {
        Ray ray1 = new Ray(transform.position + Vector3.up * 0.5f, Vector3.left);
        Ray ray2 = new Ray(transform.position + Vector3.up * 0.5f, Vector3.right);
        if (Physics.Raycast(ray2, 100, StoneLayer | layer | treeLayer))
        {
            IsOverlap = true;
            SetPosition(new Vector3(maxXZ - 6, OrigPos.y, OrigPos.z));
        }
        else
        {
            if (Physics.Raycast(ray1, 100, StoneLayer | layer | treeLayer))
            {
                SetPosition(new Vector3(maxXZ + 6, OrigPos.y, OrigPos.z));
                IsOverlap = true;
            }
            else
            {
                SetPosition(new Vector3(maxXZ, OrigPos.y, OrigPos.z));
            }
        }
    }
}
